﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Clients;
using WMS.ClientService;
using WMS.ClientService.Dto;
using WMS.Common;

namespace WMS.CilentServicess
{
    public class ClientServices : ApplicationService, IClientServices
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<Client, int> clientsRepository;

        public ClientServices(IRepository<Client, int> clientsRepository)
        {
            this.clientsRepository = clientsRepository;
        }

        /// <summary>
        /// 获取客户数据
        /// </summary>
        /// <param name="chineseName"></param>
        /// <param name="contractNumber"></param>
        /// <param name="code"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetCilentsdata(string? chineseName, string? contractNumber, string? code, int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<Client>(true);
            if (!string.IsNullOrEmpty(chineseName))
            {
                predicate = predicate.And(m => m.ChineseName.Contains(chineseName));
            }
            if (!string.IsNullOrEmpty(contractNumber))
            {
                predicate = predicate.And(m => m.ContractNumber.Contains(contractNumber));
            }
            if (!string.IsNullOrEmpty(code))
            {
                predicate = predicate.And(m => m.Code.Contains(code));
            }
            var clientlist = clientsRepository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.CurrentPage = index;
            result.PageSize = limit;
            result.RowCount = clientlist.Count();
            result.Queryable = clientlist.OrderBy(x => x.Id).Skip((index - 1) * limit).AsQueryable();
            result.Queryable = result.Queryable.MapToList<SearchClientdto>().AsQueryable();
            return result;
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="addclientdto"></param>
        /// <returns></returns>
        public async Task<int> AddClients(Addclientdto addclientdto)
        {
            var clientmodel = addclientdto.MapTo<Client>();
            await clientsRepository.InsertAsync(clientmodel);
            return 1;
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteCilents(int id)
        {
            await clientsRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 通过id获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SearchClientdto> SelectByidClient(int id)
        {
            var entity=clientsRepository.GetAsync(id).Result;
            var clientdto=entity.MapTo<SearchClientdto>();
            return clientdto;
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="searchclientdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateClients(SearchClientdto searchclientdto)
        {
            var entity= searchclientdto.MapTo<Client>();
            await clientsRepository.UpdateAsync(entity);
            return 1;
        }
    }
}
